﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.BarCode;
using ECare.BUS.Data;

namespace ECare.BUS.Workers
{
    /// <summary>
    /// 生成 原材料待检单 </summary>
    public class InReportBuilder : BackgroundWorker
    {
        public InReportBuilder()
        {
            this.DoWork += InReportBuilder_DoWork;
        }

        public SupplyInfo CurrentSupplyInfo { get; set; }

        void InReportBuilder_DoWork(object sender, DoWorkEventArgs e)
        {
            string currentDir = System.Environment.CurrentDirectory + @"\print\";

            if (!Directory.Exists(currentDir))
                Directory.CreateDirectory(currentDir);

            string filename = DateTime.Now.ToString( "yyyyMMddhhmmss");

            string desitionhtml = currentDir + filename + ".htm";
            string barcodepng = currentDir + filename + ".png";


            //生成barcode...
            var barcodeBuilder = new BarCodeBuilder
            {
                CodeText = CurrentSupplyInfo.supply_code,
                SymbologyType = Symbology.Code128,
                Resolution = new Resolution(100f, 200f, ResolutionMode.Customized)
            };
            barcodeBuilder.Save(barcodepng, ImageFormat.Png);


            string html = string.Empty;
            html = @"<html>";
            //header
            html = html + @"<header><title>原材料待检单</title></header>"; //the header is the scan converts Id
            html = html + System.Environment.NewLine;
            //body
            html = html + "<body>";
            html = html + System.Environment.NewLine;
            //show the LOGO and title
            html = html + "<TABLE align='center' cellSpacing=0 cellPadding=0 width=\"100%\" border=1 style=\"font-family: 'Calibri'; font-size: 12pt;\">";
            html = html + System.Environment.NewLine;

            html = html + "<TR>";
            html = html + "<td><strong>批次</strong></td>";
            html = html + string.Format("<td>{0}</td>", CurrentSupplyInfo.supply_code);
            html = html + "<td><strong>物料</strong></td>";
            html = html + string.Format("<td>{0}</td>", ResourcesHelper.GetResourceName(CurrentSupplyInfo.resource_id));

            html = html + "<td><strong>供应商</strong></td>";
            html = html + string.Format("<td>{0}</td>", ResourceSupplyHelper.GetSupplyNameById(CurrentSupplyInfo.resource_supply_id));
            html = html + "</TR>";

            html = html + "<TR>";
            html = html + "<td><strong>数量</strong></td>";
            html = html + string.Format("<td>{0} {1}</td>", CurrentSupplyInfo.supply_number, CurrentSupplyInfo.supply_unit);

            html = html + "<td><strong>单位数量</strong></td>";
            html = html + string.Format("<td>{0}</td>", CurrentSupplyInfo.supply_kg);

            html = html + "<td><strong>总重量</strong></td>";
            html = html + string.Format("<td>{0} 公斤</td>", (CurrentSupplyInfo.supply_kg * CurrentSupplyInfo.supply_number).ToString("F2"));
            html = html + "</TR>";

            html = html + "<TR>";
            html = html + "<td><strong>录入人</strong></td>";
            html = html + string.Format("<td>{0} {1}</td>", UserInfoHelper.GetUserNameById(CurrentSupplyInfo.supply_create_by), CurrentSupplyInfo.supply_create_time);

            html = html + "<td><strong>审核人</strong></td>";
            html = html + string.Format("<td>{0} {1}</td>", UserInfoHelper.GetUserNameById(CurrentSupplyInfo.supply_check_by), CurrentSupplyInfo.supply_check_time);

            html = html + "<td><strong>审核状态</strong></td>";
            //    html = html + string.Format("<td>{0}</td>", CurrentSupplyInfo.supply_checkstatus==CheckStatus.Qualitified?"审核通过":CurrentSupplyInfo.supply_checkstatus==CheckStatus.Rejected?"审核失败");

            if (CurrentSupplyInfo.supply_checkstatus == CheckStatus.Qualitified)
            {
                html = html + string.Format("<td><font color='green'>{0}</font></td>", "检验通过");
            }
            else if (CurrentSupplyInfo.supply_checkstatus == CheckStatus.Rejected)
            {
                html = html + string.Format("<td><font color='red'>{0}</font></td>", "检验失败");
            }
            else
            {
                html = html + string.Format("<td>{0}</td>", "未检验");
            }

            html = html + "</TR>";

            html = html + "<TR>";html = html + string.Format("<TD>备注</TD><TD colspan='5'>{0}</TD>", CurrentSupplyInfo.supply_remark);
            html = html + "</TR>";
            html = html + "<TR>";
            html = html + string.Format("<TD colspan='6' align='center'><image src='{0}'/></TD>", barcodepng);
            html = html + "</TR>";


            html = html + "</table>";
            html = html + "</body>";html = html + System.Environment.NewLine;
            html = html + "</html>";

            File.WriteAllText(desitionhtml, html,Encoding.GetEncoding("gb2312"));
            e.Result = desitionhtml;

        }
    }
}
